﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Authorization.Properties;

namespace Authorization
{
   partial class AuthorizationServiceBase
    {

        #region RoleDefinitions Members


        public RoleDefinitionCollection GetAllRoleDefinitions(string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");

            var application = AuthorizationStoreManager.GetApplication(applicationName);
            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.GetAllRoleDefinitions", Resources.invalid_application);
            }
            return application.GetRoleDefinitions(scopeName);
        }

        public string[] GetAllRoleDefinitionNames(string applicationName, string scopeName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");

            var app = AuthorizationStoreManager.GetApplication(applicationName);

            if (app == null)
            {
                throw new AuthorizationException("AuthorizationService.GetAllRoleDefinitionNames", Resources.invalid_application);
            }

            return app.GetRoleDefinitionNames(scopeName);
        }

        public RoleDefinition GetRoleDefinition(string applicationName, string scopeName, string roleDefinitionName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref roleDefinitionName, true, true, false, 64, "roleDefinitionName");

            var app = AuthorizationStoreManager.GetApplication(applicationName);

            if (app == null)
            {
                throw new AuthorizationException("AuthorizationService.GetRoleDefinition", Resources.invalid_application);
            }

            return app.GetRoleDefinition(roleDefinitionName, scopeName);
        }

        public bool RoleDefinitionExists(string applicationName, string scopeName, string roleDefinitionName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref roleDefinitionName, true, true, false, 64, "roleDefinitionName");

            var app = AuthorizationStoreManager.GetApplication(applicationName);

            if (app == null)
            {
                throw new AuthorizationException("AuthorizationService.RoleDefinitionExists", Resources.invalid_application);
            }

            return app.RoleDefinitionExists(roleDefinitionName, scopeName);
        }

        public RoleDefinition CreateRoleDefinition(string applicationName, string scopeName, string roleDefinitionName, string description)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref roleDefinitionName, true, true, false, 64, "roleDefinitionName");

            Application application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.CreateRoleDefinition", Resources.invalid_application);
            }

            var role = application.CreateRoleDefinition(roleDefinitionName, description, scopeName);
            role.Save();
            return role;
        }

        public void DeleteRoleDefinition(string applicationName, string scopeName, string roleDefinitionName)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref roleDefinitionName, true, true, false, 64, "roleDefinitionName");

            var application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.DeleteRoleDefinition", Resources.invalid_application);
            }

            application.DeleteRoleDefinition(roleDefinitionName, scopeName);
            application.Save();
        }

        public void UpdateRoleDefinition(string applicationName, string scopeName, string roleDefinitionName, string newRoleDefinitionName, string newDescription)
        {
            SecUtility.CheckParameter(ref applicationName, true, true, false, 64, "applicationName");
            SecUtility.CheckParameter(ref roleDefinitionName, true, true, false, 64, "roleDefinitionName");
            SecUtility.CheckParameter(ref newRoleDefinitionName, true, true, false, 64, "newRoleDefinitionName");

            var application = AuthorizationStoreManager.GetApplication(applicationName);

            if (application == null)
            {
                throw new AuthorizationException("AuthorizationService.UpdateRoleDefinition", Resources.invalid_application);
            }

            var role = application.UpdateRoleDefinition(roleDefinitionName, scopeName, newRoleDefinitionName, newDescription);
            role.Save();
        }

        #endregion

    }
}
